package cn.kgc.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 课工场
 * @date 2024/4/25
 * @description  创建 接口的代理对象
 */
public class MybatisUtil {

    private static SqlSessionFactory factory;

    private MybatisUtil(){}

    static {
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //创建代理对象
    public  static   <T> T   getMapperProxy(Class<T> type){
        SqlSession sqlSession = factory.openSession(true);
        return   sqlSession.getMapper(type);
    }

}
